package org.gcb.core.ui.nodes;

import java.awt.datatransfer.Transferable;
import java.beans.IntrospectionException;
import java.io.IOException;
import java.util.List;
import org.gcb.core.model.Equipment.Carried;
import org.gcb.core.model.Item;
import org.openide.nodes.AbstractNode;
import org.openide.nodes.BeanNode;
import org.openide.nodes.ChildFactory;
import org.openide.nodes.Children;
import org.openide.nodes.Node;
import org.openide.util.Exceptions;
import org.openide.util.lookup.Lookups;

/**
 *
 * @author Jason
 */
public class EquipmentCarriedChildFactory extends ChildFactory<Item> {

    private List<Item> bag;

    public EquipmentCarriedChildFactory(Carried carried) {
        this.bag = carried.subList(0, carried.size());
    }

    @Override
    protected boolean createKeys(List<Item> toPopulate) {
        for (Item iItem : bag) {
            toPopulate.add(iItem);
        }
        return true;
    }

    @Override
    protected Node createNodeForKey(final Item key) {
        Node node;
        try {
            node = new BeanNode(key) {

                @Override
                public Transferable drag() throws IOException {
                    return key;
                }

            };
        } catch (IntrospectionException ex) {
            node = new AbstractNode(Children.LEAF, Lookups.singleton(key)) {

                @Override
                public Transferable drag() throws IOException {
                    return key;
                }

            };
        }
        node.setDisplayName(key.getText());
        node.setShortDescription(key.getDescription());
        return node;
    }
}
